package com.shms.action;


import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfGrade;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShGrade;
import com.shms.service.ShGradeService;

public class GradeAction extends ActionSupport {
	private static final long serialVersionUID = -7106008904232245508L;
	private ShGradeService shGradeService;
	public GroupOfGrade groupOfGrade;
	public ShGrade shGrade;
	private Integer[] nos;
	public String message;
	public Integer[] getNos() {
		return nos;
	}
	public void setNos(Integer[] nos) {
		this.nos = nos;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	/**
	 * 根据分页信息，查询年级列表
	 * @return 
	 */
	public String queryGrades() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//构造查询动态信息的分页信息输入参数
		GroupOfGrade gradeParams = new GroupOfGrade();
		gradeParams.setCurrentPage(currentPage);
		gradeParams.setType(type);
		gradeParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfGrade = shGradeService.queryGrade(gradeParams);
		if (groupOfGrade.getShGrade() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	/**
	 * 批量删除动态信息
	 * @return 
	 */
	public String deleteGrades() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			ShGrade shGrade =new ShGrade();
			shGrade.setGid(Long.parseLong(ServletActionContext.getRequest().getParameter("no")));
			if(shGradeService.queryClassesById(shGrade).size()>0){
				message="已经存在班级的年级数据，不允许删除";
				//获取传来的当前页数
				Integer currentPage = groupOfGrade.getCurrentPage() - 1;
				//获取传来的上一页或下一页标志
				String type = groupOfGrade.getType();
				//构造查询动态信息的分页信息输入参数
				GroupOfGrade gradeParams = new GroupOfGrade();
				gradeParams.setCurrentPage(currentPage);
				gradeParams.setType(type);
				gradeParams.setPageSize(QueryConstant.F_Pagesize);
				//查询
				groupOfGrade = shGradeService.queryGrade(gradeParams);
				return INPUT;
			}
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shGradeService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage = groupOfGrade.getCurrentPage() - 1;
			//获取传来的上一页或下一页标志
			String type = groupOfGrade.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfGrade gradeParams = new GroupOfGrade();
			gradeParams.setCurrentPage(currentPage);
			gradeParams.setType(type);
			gradeParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfGrade = shGradeService.queryGrade(gradeParams);
			return SUCCESS;
		}else{
			message = "删除动态信息失败";
			return INPUT;
		}
	}
	/**
	 * 查询单个年级信息
	 * @return
	 */
	public String queryByGradeId() {
		//获取要查询的单条记录id
		long id = Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		ShGrade shGradeParams = new ShGrade();
		shGradeParams.setGid(id);
		shGrade = shGradeService.queryById(shGradeParams);
		return SUCCESS;
	}
	
	/**
	 * 添加年级
	 * @return
	 */
	public String addGrades() {
		return SUCCESS;
	}
	/**
	 * 保存年级信息
	 * @return
	 */
	public String saveGrades() {
		boolean rtn = shGradeService.saveGrades(shGrade);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		return SUCCESS;
	}
	public ShGradeService getShGradeService() {
		return shGradeService;
	}
	public void setShGradeService(ShGradeService shGradeService) {
		this.shGradeService = shGradeService;
	}
	public GroupOfGrade getGroupOfGrade() {
		return groupOfGrade;
	}
	public void setGroupOfGrade(GroupOfGrade groupOfGrade) {
		this.groupOfGrade = groupOfGrade;
	}
	public ShGrade getShGrade() {
		return shGrade;
	}
	public void setShGrade(ShGrade shGrade) {
		this.shGrade = shGrade;
	}

}
